
import { NOT_LOGIN_CODE } from './constants.js';
const web_root = 'https://shop.rcglife.com/';
export const img_root = 'http://img.rcglife.com/';
// 首页
export const GET_PIC_WALL = "/api/getPicwall";                       // 轮播图
export const GET_HOME_CATE = "/api/getHomeCate";                     // 首页分类
export const GET_CATE = "/api/getCate";                              // 获取分类
export const GET_CUR_TIME_BUY = "/api/getCurTimeBuy";                // 获取当前抢购商品
export const GET_TIME_BUY_LIST = "/api/getTimeBuyList";             // 抢购活动列表
export const GET_TIME_BUY_GOODS_LIST = "/api/getTimeBuyGoodsList";   // 抢购活动商品列表
export const GET_USER_INFO = '/api/getUserInfo'; //获取用户信息
export const LOGIN = '/api/login';
export const GET_HOT_CATE_GOODS = '/api/getHotCateGoods'; // 获取分类热推商品
export const GET_CHILD_CATE = "/api/getChildCate"; //获取子类和商品信息
export const GET_HOME_GOODS = "/api/getHomeGoods"; //获取首页商品
export const GET_GOODS_DETAIL = "/api/getGoodsDetail" ; //获取商品详情
export const GET_GOODS_INFO_BY_SPEC = "/api/getGoodsInfoBySpec"; //根据规格获取数据
export const ADD_CART = "/api/addCart" ; //加入购物车
export const GET_CART = "/api/getCart";  //获取购物车
export const GOODS_SELECT = "/api/goodsSelect";//商品选中
export const UPDATE_GOODS_NUM = "/api/updateGoodsNum"; //更新购物车商品数量
export const REMOVE_CART = "/api/removeCart"; //移除购物车
export const SELECT_ALL = "/api/selectAll"; //选中所有
export const DO_PAY_LIST = "/api/doPayList"; //待支付列表
export const CREATE_ORDER = "/api/createOrder"; //创建订单
export const PAY_ORDER = "/api/payOrder" ; //支付订单
export const SEARCH_GOODS = "/api/searchGoods"; //搜索商品
export const GET_ORDER_LIST = "/api/getOrderList"; //获取订单列表
export const GOODS_COLLECT = "/api/goodsCollect"; //商品收藏
export const GET_CATE_GOODS = "/api/getCateGoods"; //获取商品分类
export const GET_MY_COLLECT = "/api/getMyCollect"; //获取我的收藏商品
export const SET_ADDRESS = "/api/setAddress"; //设置我的收货地址
export const GET_DEFAULT_ADDR = "/api/getDefaultAddr"; //获取默认收货地址
export const SET_GOODS_HISTORY = "/api/setGoodsHistory"; //设置商品浏览足迹
export const GET_GOODS_HISTORY = "/api/getGoodsHistory"; //获取我的足迹
export const CANCEL_ORDER = "/api/cancelOrder"; //取消订单
export const GET_OSS_CONFIG = "/api/getOssConfig"; //获取oss上传配置文件
export const GET_SHOP_ORDER_GOODS_INFO = "/api/getShopOrderGoodsInfo"; //订单商品详情
export const ADD_COMMENT = "/api/addComment"; //添加评论
export const GET_GOODS_COMMENT = "/api/getGoodsComment"; //获取商品评论
export const GET_ALL_COMMENTS = "/api/getAllComments"; //获取所有评论
export const CONFIRM_ORDER = "/api/confirmOrder"; //确认收货
export const SET_PREPAYID = "/api/setPrepayid"; //设置交易标识
export const FEEDBACK = "/api/feedback"; //意见反馈
export const ADD_SUBSCRIBE = "/api/addSubscribe"; //添加提醒
export const GET_ORDER_DETAIL = "/api/getOrderDetail"; //获取订单详情

export default {
    // POST
    request(url, params, cb, onload) {
        let _this = this;
        wx.request({
            url: web_root + url,
            method: 'POST',
            data: params,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                let code = res.data.c;

                if (code == 1) {
                    //正常请求
                    cb && cb(res.data.d);
                } else if (code == NOT_LOGIN_CODE) {
                    //未登录
                    // _this.login();
                } else {
                    //接口请求错误
                    wx.showModal({
                        showCancel: false,
                        content: res.data.m,
                    })
                }

            }
        })

    },
    fetch(url, params, cb, errorCb) {
        this.request(url, params, cb, errorCb);
    },
    login(cb) {
        let _this = this;
        wx.login({
            success: function (res) {
                if (res.code) {
                    var code = res.code;
                    wx.getUserInfo({
                        withCredentials: true,
                        success: function (res) {
                            var encryptedData = res.encryptedData;//一定要把加密串转成URI编码
                            var iv = res.iv;
                            //请求自己的服务器
                            _this.fetch(LOGIN, { iv: iv, code: code, encryptedData: encryptedData }, (resp) => {
                                wx.setStorageSync('login', resp);
                                wx.setStorageSync('login_time', new Date().getTime());
                                cb && cb();
                            })
                        }
                    })
                }
            }
        })
    },
    checkLogin(cb){
        let _this = this,
            key = wx.getStorageSync('login'),
            login_time = wx.getStorageSync('login_time'),
            second = 0;

        if (login_time) {
            second = parseInt((new Date().getTime() - login_time) / 1000);
            if (second > 21600) {
                _this.login(cb);
                //登录超时 重新登录
            } else {
                return true;
            }
        } else {
            _this.login(cb);
        }
    },
    getKey() {
        return wx.getStorageSync('login');
    }
}